package com.tianji.learning.mq;


import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.PointsMessage;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.tianji.common.constants.MqConstants.Exchange.LEARNING_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.*;
import static com.tianji.common.constants.MqConstants.Queue.*;

@Slf4j
@Component
@RequiredArgsConstructor
public class LearningPointRecordChangeListener {
    private final IPointsRecordService pointsRecordService;

    /**
     * 01 添加积分记录 -> 监听每日签到 （day07-随堂-03）
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = SIGN_POINTS_QUEUE, durable = "true"),
            exchange = @Exchange(name = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = SIGN_IN
    ))
    public void listenAddSignPoints(PointsMessage message) {
        // 1 健壮性判断

        // 2 添加到 积分记录表
        log.info("监听到每日签到消息，添加积分记录。msg：{}", message);
        pointsRecordService.addPointsRecord(message.getUserId(), message.getPoints(), PointsRecordType.SIGN);
    }

    /**
     * 02 添加积分记录 -> 监听新增评论 （day07-随堂-03）
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QA_POINTS_QUEUE, durable = "true"),
            exchange = @Exchange(name = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = WRITE_REPLY
    ))
    public void listenAddReplyPoints(PointsMessage message) {
        // 1 健壮性判断
        // 2 添加到 积分记录表
        log.info("监听到新增评论消息，添加积分记录。msg：{}", message);
        pointsRecordService.addPointsRecord(message.getUserId(), message.getPoints(), PointsRecordType.QA);
    }
    /**
     * 02 添加积分记录 -> 监听新增评论 （day07-随堂-03）
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = LEARN_SECTION_POINTS_QUEUE, durable = "true"),
            exchange = @Exchange(name = LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = LEARN_SECTION
    ))
    public void listenLearningRecordPoints(PointsMessage message) {
        // 1 健壮性判断
        // 2 添加到 积分记录表
        log.info("监听到新增评论消息，添加积分记录。msg：{}", message);
        pointsRecordService.addPointsRecord(message.getUserId(), message.getPoints(), PointsRecordType.LEARNING);
    }


}
